package com.dmx.cep.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dmx.cep.dao.ContentDao;
import com.dmx.cep.entity.AdiImportTask;
import com.dmx.cep.entity.FtpServerConfig;
import com.dmx.cep.entity.VodContentInfo;
import com.dmx.cep.entity.VodContentStatus;
import com.dmx.cep.exception.ObjectNotFoundException;
import com.dmx.cep.ws.datamodel.ContentStatus;
import com.dmx.fw.dao.hibernateimpl.BaseDaoImpl;

public class ContentDaoImpl extends BaseDaoImpl implements ContentDao {

	public List<AdiImportTask> getAdiImportTasks(final String siteId,final int maxRecords) {

		return (List<AdiImportTask>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hsql = "from AdiImportTask task where task.status=0 and task.siteId=? order by task.creationTime";
						
						Query query=session.createQuery(hsql);
						//Query query=session.getNamedQuery("getAdiImportTasks");
						query.setParameter(0, siteId);
						query.setFirstResult(0);
						query.setMaxResults(maxRecords);
						return query.list();
					}
				});
	}
	
	public FtpServerConfig  getFtpServer(int ftpType){
		List<FtpServerConfig> ftpList=this.getHibernateTemplate().find("from FtpServerConfig fc where fc.type=? and fc.status=1",new Object[]{ftpType});
		
		if(ftpList!=null&&ftpList.size()>0){
			return ftpList.get(0);
		}
		
		return null;
	}

	@Override
	public void contentDelete(String[] contentIDList) throws DataAccessException {
		// TODO Auto-generated method stub
		for(String contentId:contentIDList){
			VodContentInfo contentInfo=(VodContentInfo)this.getHibernateTemplate().get(VodContentInfo.class, contentId);
			
			//if(contentInfo==null) throw new ObjectNotFoundException("Object not found with assetId:"+contentId);
			
			if(contentInfo!=null&&contentInfo.getStatus()!=-1){
				contentInfo.setStatus(-1);
				
				this.getHibernateTemplate().update(contentInfo);
				
				VodContentStatus contentStatus=(VodContentStatus)this.getHibernateTemplate().get(VodContentStatus.class, contentId);
				if(contentStatus!=null){
					contentStatus.setIsValid(0);
					contentStatus.setLastUpdateTime(new Date());
					this.getHibernateTemplate().update(contentStatus);
				}
			}
		}
	}
	
	@Override
	public void deleteContent(String[] contentIDList) throws DataAccessException {
		// TODO Auto-generated method stub
		for(String contentId:contentIDList){
			VodContentInfo contentInfo=(VodContentInfo)this.getHibernateTemplate().get(VodContentInfo.class, contentId);
			
			//if(contentInfo==null) throw new ObjectNotFoundException("Object not found with assetId:"+contentId);
			if(contentInfo!=null){
				if(contentInfo.getStatus()!=-1){
					contentInfo.setStatus(-1);
					
					this.getHibernateTemplate().update(contentInfo);
					
					VodContentStatus contentStatus=(VodContentStatus)this.getHibernateTemplate().get(VodContentStatus.class, contentId);
					if(contentStatus!=null){
						contentStatus.setIsValid(0);
						contentStatus.setLastUpdateTime(new Date());
						this.getHibernateTemplate().update(contentStatus);
					}
					
				}else{
					VodContentStatus contentStatus=(VodContentStatus)this.getHibernateTemplate().get(VodContentStatus.class, contentId);
					
					if(contentStatus!=null&&contentStatus.getIsValid()==-1){
						contentStatus.setIsValid(-3);  //重试
						contentStatus.setLastUpdateTime(new Date());
						this.getHibernateTemplate().update(contentStatus);
					}
				}
			}

		}
	}	

	@Override
	public void contentUpdate(ContentStatus[] contentStatusList) throws DataAccessException {
		// TODO Auto-generated method stub
		for(ContentStatus content:contentStatusList){
			VodContentInfo contentInfo=(VodContentInfo)this.getHibernateTemplate().get(VodContentInfo.class, content.getContentId());
			
			//if(contentInfo==null) throw new ObjectNotFoundException("Object not found with assetId:"+content.getContentId());
			
			if(contentInfo!=null&&contentInfo.getStatus()!=-1){
				contentInfo.setStatus(content.getStatus());
				contentInfo.setCreationDate(new Date());
				this.getHibernateTemplate().update(contentInfo);
			}
		}
	}
	
	public int getAssetStatus(String assetId) throws DataAccessException{
		
		List<VodContentStatus> contentStatusList=this.getHibernateTemplate().find("from VodContentStatus s where s.contId=?",new Object[]{assetId});
		
		if(contentStatusList==null||contentStatusList.size()==0) throw new ObjectNotFoundException("Object not found with assetId:"+assetId);
		
		return contentStatusList.get(0).getAssetStatus();
	}

	@Override
	public List<Object[]> getContentTypeList() throws Exception {
		// TODO Auto-generated method stub
		return (List<Object[]>)getHibernateTemplate().execute(
				new HibernateCallback(){
					public Object doInHibernate(Session session) throws HibernateException, SQLException{
						String sql="select GENREID,GENRE from t_vod_content_type where FLAG=1";
						
						SQLQuery query=session.createSQLQuery(sql);
						
						return query.list();
					}
				}
		);
	}

	@Override
	public int countCaptionTask() throws Exception {
		// TODO Auto-generated method stub
		List<Long> result=this.getHibernateTemplate().find("select count(s.contId) from VodContentStatus s where s.captionStatus=3 or s.captionStatus=-1 ");
		
		return result.get(0).intValue();
	}
}
